#include "../views.hpp"

using lms::views::TurnView;

TurnView::TurnView()
{  
  queueCustomer = new Queue< Customer >();
  UnitOfWork& unitOfWork = UnitOfWork::getInstance();
  customerServices = unitOfWork.GetCustomerServices(); 
  option = 0;
}

void TurnView::addCustomerToQueue()
{   
  Utilities::clearScreen();
  string clientName = Utilities::reinterpret_input< string >( "\n\tType the Client Name.: " );
  Customer* retrievedClient = customerServices->getByName( clientName ); 
  
  if( retrievedClient != null )  
  {
    queueCustomer->enQueue( retrievedClient );    
    Utilities::printText("\n\tClient Added to Queue.");
  }
  else
  {
    createIfNotExist();
    Utilities::printText("\n\tClient Created and Added to Queue.");    
  }
  
  Utilities::pause();
}

Customer* TurnView::attendCustomer()
{
  return (queueCustomer->deQueue())->getData();  
}

void TurnView::createIfNotExist()
{
  CustomerView* customerView = new CustomerView();
  customerView->show();
}

Queue< Customer >* TurnView::getQueueCustomers()
{
  return queueCustomer;
}

void TurnView::listAll()
{
  
  Utilities::clearScreen();
  
  Node< Customer >* first = queueCustomer->getFirst();
  while( first != null )
  {
    Customer* temp = first->getData();
    if( temp !=null )
    {
      Utilities::printText( temp->getFullName() );
    }
    first = first->getNext();
  }
  
  Utilities::pause();
}

void TurnView::show(){
   
    do{
  Utilities::clearScreen();
  Utilities::printText("\t Management Of Turns");
  Utilities::printText( "\n\t 1-Add Queue." );
  Utilities::printText( "\n\t 2-List All." );
  Utilities::printText( "\n\t 3-Exit." );
  option = Utilities::reinterpret_input< int >( "\n\tChoose an option: " );
  switch( option )
  {
    case 1:
      addCustomerToQueue();
      break;
    case 2:
      listAll();
      break;
    case 3:
      break;
    default:
        Utilities::printText("\tInvalid Option\n");
        Utilities::pause();
      break;
  }
 }while(option != 3);
}
